﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Ttifa.Infrastructure;
using Ttifa.WebApiBase.Filters;

namespace Ttifa.WebApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API 配置和服务
            //config.Services.Replace(typeof(IHttpActionResult), new ApiInvoker());

            // 路由前的验证
            config.MessageHandlers.Add(new IllegalCharsHandler());
            config.MessageHandlers.Add(new HttpMethodOverrideHandler());

            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "MvcApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            //自定义json格式化
            config.Formatters.JsonFormatter.SerializerSettings = DefaultSettings.JsonSerializerSettings;

            // Web API 过滤器
            config.Filters.Add(new ApiErrorAttribute());

            // API返回数据处理：Json/Jsonp/String
            //config.Services.Replace(typeof(IContentNegotiator), new ApiContentNegotiator());
        }
    }
}
